package controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;
import javax.ejb.EJB;
import entity.MSpecialist;
import entity.MHotel;
import session.MSpecialistFacade;
import session.MHotelFacade;


@WebServlet(name = "ShareController",
loadOnStartup = 1,
urlPatterns = {"/Share/getSpecialist",
    "/Share/getHotel"
    })
/**
 *
 * @author PARINYA
 */
public class ShareController extends HttpServlet {
 private String sampleServletContext;

    @EJB
    private MHotelFacade mHotelFacade;
    @EJB
    private MSpecialistFacade mSpecialistFacade;
    

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

        super.init(servletConfig);
        sampleServletContext = servletConfig.getServletContext().getInitParameter("sampleServletContext");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();

        if (userPath.equals("/Share/getSpecialist")) {
            response.setContentType("text/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            List<MSpecialist> lMSpecialist;
            Map mapSpeialist = null;
            List listSpeialist = new LinkedList();
            String strJson = "";

            try {
                //Query Java Object
                Gson gson = new Gson();
                lMSpecialist = mSpecialistFacade.LoadDataSpecialist( "", "", "id", "asc");
                if (!lMSpecialist.isEmpty()) {
                    for (MSpecialist obj : lMSpecialist) {
                        mapSpeialist = new LinkedHashMap();
                        mapSpeialist.put("name",obj.getName());
                        mapSpeialist.put("value",obj.getId());
                        listSpeialist.add(mapSpeialist);
                    }
                }

                //@TODO : Add Json Value
                strJson = gson.toJson(listSpeialist);//JSONValue.toJSONString(listDoctor);
                out.println(strJson);
            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            } finally {
                out.close();
                lMSpecialist = null;
                strJson = null;
            }
            isDispatcher = false;
        }else if (userPath.equals("/Share/getHotel")) {
            response.setContentType("text/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            List<MHotel> lMHotel;
            Map mapHotel = null;
            List listHotel = new LinkedList();
            String strJson = "";

            try {
                //Query Java Object
                Gson gson = new Gson();
                lMHotel = mHotelFacade.LoadDataHotel("", "", "", "id", "asc");
                if (!lMHotel.isEmpty()) {
                    for (MHotel obj : lMHotel) {
                        mapHotel = new LinkedHashMap();
                        mapHotel.put("name",obj.getName());
                        mapHotel.put("value",obj.getId());
                        listHotel.add(mapHotel);
                    }
                }

                //@TODO : Add Json Value
                strJson = gson.toJson(listHotel);//JSONValue.toJSONString(listDoctor);
                out.println(strJson);
            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            } finally {
                out.close();
                lMHotel = null;
                strJson = null;
            }
            isDispatcher = false;
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



}
